<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>应用 API | Vue.js</title>
    <meta name="generator" content="VuePress 1.8.0">
    <link href="https://fonts.googleapis.com/css?family=Inter:300,400,500,600|Open+Sans:400,600;display=swap" rel="stylesheet">
    <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <link rel="icon" href="/logo.png">
    <link rel="manifest" href="/manifest.json">
    <link rel="apple-touch-icon" href="/images/icons/apple-icon-152x152.png">
    <script src="https://player.youku.com/iframeapi"></script>
    <meta name="description" content="Vue.js - The 渐进式 JavaScript 框架">
    <meta name="theme-color" content="#3eaf7c">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="msapplication-TileImage" content="/images/icons/ms-icon-144x144.png">
    <meta name="msapplication-TileColor" content="#000000">
    
    <link rel="preload" href="/assets/css/0.styles.dbd5a077.css" as="style"><link rel="preload" href="/assets/js/app.8e9a370d.js" as="script"><link rel="preload" href="/assets/js/11.282d7961.js" as="script"><link rel="preload" href="/assets/js/40.5c7a2259.js" as="script"><link rel="preload" href="/assets/js/22.0b365510.js" as="script"><link rel="prefetch" href="/assets/js/1.68d5ca75.js"><link rel="prefetch" href="/assets/js/100.e9ca97cb.js"><link rel="prefetch" href="/assets/js/101.bde08412.js"><link rel="prefetch" href="/assets/js/102.de4df251.js"><link rel="prefetch" href="/assets/js/103.a7e77998.js"><link rel="prefetch" href="/assets/js/104.bcb1d675.js"><link rel="prefetch" href="/assets/js/105.e433ec30.js"><link rel="prefetch" href="/assets/js/106.cce18a4f.js"><link rel="prefetch" href="/assets/js/107.b4b07388.js"><link rel="prefetch" href="/assets/js/108.b839e5aa.js"><link rel="prefetch" href="/assets/js/109.36fbdd83.js"><link rel="prefetch" href="/assets/js/110.973c3515.js"><link rel="prefetch" href="/assets/js/111.f6de6497.js"><link rel="prefetch" href="/assets/js/112.01569df8.js"><link rel="prefetch" href="/assets/js/113.6a3748b2.js"><link rel="prefetch" href="/assets/js/114.e98d382e.js"><link rel="prefetch" href="/assets/js/115.1e1c5889.js"><link rel="prefetch" href="/assets/js/116.f02cbdf7.js"><link rel="prefetch" href="/assets/js/117.0416f584.js"><link rel="prefetch" href="/assets/js/118.6f3e5b86.js"><link rel="prefetch" href="/assets/js/119.39bbe5d8.js"><link rel="prefetch" href="/assets/js/12.8ce387e5.js"><link rel="prefetch" href="/assets/js/120.7b14cee5.js"><link rel="prefetch" href="/assets/js/121.d204ab46.js"><link rel="prefetch" href="/assets/js/122.89d04b6b.js"><link rel="prefetch" href="/assets/js/123.9fce5790.js"><link rel="prefetch" href="/assets/js/124.32117a20.js"><link rel="prefetch" href="/assets/js/125.7790c8fb.js"><link rel="prefetch" href="/assets/js/126.346e0c66.js"><link rel="prefetch" href="/assets/js/127.90aecff9.js"><link rel="prefetch" href="/assets/js/128.b310aae3.js"><link rel="prefetch" href="/assets/js/129.a541baa4.js"><link rel="prefetch" href="/assets/js/13.7ecb88c4.js"><link rel="prefetch" href="/assets/js/130.d8a83203.js"><link rel="prefetch" href="/assets/js/131.8f77a918.js"><link rel="prefetch" href="/assets/js/132.69dd48f6.js"><link rel="prefetch" href="/assets/js/133.1a17c435.js"><link rel="prefetch" href="/assets/js/134.1daf7963.js"><link rel="prefetch" href="/assets/js/135.bfe5dd93.js"><link rel="prefetch" href="/assets/js/136.7b9e2494.js"><link rel="prefetch" href="/assets/js/137.774f5233.js"><link rel="prefetch" href="/assets/js/138.5b9c62da.js"><link rel="prefetch" href="/assets/js/139.ea828073.js"><link rel="prefetch" href="/assets/js/14.1e94d35c.js"><link rel="prefetch" href="/assets/js/140.eb5d606e.js"><link rel="prefetch" href="/assets/js/141.74f9ddd6.js"><link rel="prefetch" href="/assets/js/142.e6a6c183.js"><link rel="prefetch" href="/assets/js/143.a862768e.js"><link rel="prefetch" href="/assets/js/144.11e984d3.js"><link rel="prefetch" href="/assets/js/145.5a2b45fa.js"><link rel="prefetch" href="/assets/js/146.11a6e459.js"><link rel="prefetch" href="/assets/js/147.2d731ce5.js"><link rel="prefetch" href="/assets/js/148.10b05806.js"><link rel="prefetch" href="/assets/js/149.dbd18e85.js"><link rel="prefetch" href="/assets/js/15.5f97d7d3.js"><link rel="prefetch" href="/assets/js/150.5d29b06e.js"><link rel="prefetch" href="/assets/js/151.f95a5312.js"><link rel="prefetch" href="/assets/js/152.6356d359.js"><link rel="prefetch" href="/assets/js/153.048fb9f5.js"><link rel="prefetch" href="/assets/js/154.b696a927.js"><link rel="prefetch" href="/assets/js/155.cbab8e0c.js"><link rel="prefetch" href="/assets/js/156.8b6dfeaa.js"><link rel="prefetch" href="/assets/js/157.ddb9630f.js"><link rel="prefetch" href="/assets/js/158.8830bcae.js"><link rel="prefetch" href="/assets/js/159.73f9805a.js"><link rel="prefetch" href="/assets/js/16.99f0a46f.js"><link rel="prefetch" href="/assets/js/160.81d6e578.js"><link rel="prefetch" href="/assets/js/161.4a54be2a.js"><link rel="prefetch" href="/assets/js/162.b9e2a680.js"><link rel="prefetch" href="/assets/js/163.aa4320f9.js"><link rel="prefetch" href="/assets/js/164.ba76b659.js"><link rel="prefetch" href="/assets/js/165.1de3781c.js"><link rel="prefetch" href="/assets/js/17.1e50c7c0.js"><link rel="prefetch" href="/assets/js/18.c8e6d35f.js"><link rel="prefetch" href="/assets/js/19.6ac0efff.js"><link rel="prefetch" href="/assets/js/2.15e48903.js"><link rel="prefetch" href="/assets/js/20.22b01ff8.js"><link rel="prefetch" href="/assets/js/21.61e0ef7d.js"><link rel="prefetch" href="/assets/js/23.d257648d.js"><link rel="prefetch" href="/assets/js/24.2c5ca951.js"><link rel="prefetch" href="/assets/js/25.443a2cc7.js"><link rel="prefetch" href="/assets/js/26.4aebfb8c.js"><link rel="prefetch" href="/assets/js/27.44432e12.js"><link rel="prefetch" href="/assets/js/28.a1381a73.js"><link rel="prefetch" href="/assets/js/29.ea6a347d.js"><link rel="prefetch" href="/assets/js/3.5a036bb1.js"><link rel="prefetch" href="/assets/js/30.672c3d59.js"><link rel="prefetch" href="/assets/js/31.cd576b97.js"><link rel="prefetch" href="/assets/js/32.f82869d3.js"><link rel="prefetch" href="/assets/js/33.5e981f72.js"><link rel="prefetch" href="/assets/js/34.e5c4e496.js"><link rel="prefetch" href="/assets/js/35.34f44353.js"><link rel="prefetch" href="/assets/js/36.e6d3f5ae.js"><link rel="prefetch" href="/assets/js/37.f1486fb3.js"><link rel="prefetch" href="/assets/js/38.f7479caa.js"><link rel="prefetch" href="/assets/js/39.b7040bfe.js"><link rel="prefetch" href="/assets/js/4.28e546d1.js"><link rel="prefetch" href="/assets/js/41.d12114b6.js"><link rel="prefetch" href="/assets/js/42.c5619795.js"><link rel="prefetch" href="/assets/js/43.1aa3e372.js"><link rel="prefetch" href="/assets/js/44.8acfbe38.js"><link rel="prefetch" href="/assets/js/45.8b1b0f30.js"><link rel="prefetch" href="/assets/js/46.afc40541.js"><link rel="prefetch" href="/assets/js/47.e07b3e77.js"><link rel="prefetch" href="/assets/js/48.ab49e1e2.js"><link rel="prefetch" href="/assets/js/49.85bbd96a.js"><link rel="prefetch" href="/assets/js/5.ee0f2808.js"><link rel="prefetch" href="/assets/js/50.afaeaf27.js"><link rel="prefetch" href="/assets/js/51.6164767c.js"><link rel="prefetch" href="/assets/js/52.6b354c7b.js"><link rel="prefetch" href="/assets/js/53.8e7f9b46.js"><link rel="prefetch" href="/assets/js/54.875869bf.js"><link rel="prefetch" href="/assets/js/55.67228c1b.js"><link rel="prefetch" href="/assets/js/56.7be74d38.js"><link rel="prefetch" href="/assets/js/57.cfe3ab1d.js"><link rel="prefetch" href="/assets/js/58.ca0d6e81.js"><link rel="prefetch" href="/assets/js/59.b470025e.js"><link rel="prefetch" href="/assets/js/6.687624bc.js"><link rel="prefetch" href="/assets/js/60.59c62dd5.js"><link rel="prefetch" href="/assets/js/61.bee6f9f3.js"><link rel="prefetch" href="/assets/js/62.35a0754d.js"><link rel="prefetch" href="/assets/js/63.5a8376a0.js"><link rel="prefetch" href="/assets/js/64.f23f42f8.js"><link rel="prefetch" href="/assets/js/65.be1ec638.js"><link rel="prefetch" href="/assets/js/66.7998f627.js"><link rel="prefetch" href="/assets/js/67.d0395c0d.js"><link rel="prefetch" href="/assets/js/68.2f3fdf70.js"><link rel="prefetch" href="/assets/js/69.9aa702b9.js"><link rel="prefetch" href="/assets/js/7.ccd8a671.js"><link rel="prefetch" href="/assets/js/70.68d38a8b.js"><link rel="prefetch" href="/assets/js/71.688d1f43.js"><link rel="prefetch" href="/assets/js/72.c3da244d.js"><link rel="prefetch" href="/assets/js/73.62019e76.js"><link rel="prefetch" href="/assets/js/74.55f11e97.js"><link rel="prefetch" href="/assets/js/75.28f7a410.js"><link rel="prefetch" href="/assets/js/76.aea5fcac.js"><link rel="prefetch" href="/assets/js/77.767ba849.js"><link rel="prefetch" href="/assets/js/78.867989b0.js"><link rel="prefetch" href="/assets/js/79.18c04aab.js"><link rel="prefetch" href="/assets/js/80.d9ab8e04.js"><link rel="prefetch" href="/assets/js/81.c1b96691.js"><link rel="prefetch" href="/assets/js/82.b423128a.js"><link rel="prefetch" href="/assets/js/83.18e205da.js"><link rel="prefetch" href="/assets/js/84.029c6d49.js"><link rel="prefetch" href="/assets/js/85.d7c00a92.js"><link rel="prefetch" href="/assets/js/86.92a5416c.js"><link rel="prefetch" href="/assets/js/87.6bd41202.js"><link rel="prefetch" href="/assets/js/88.5fd01128.js"><link rel="prefetch" href="/assets/js/89.2da78840.js"><link rel="prefetch" href="/assets/js/90.0c5a649e.js"><link rel="prefetch" href="/assets/js/91.4daf663c.js"><link rel="prefetch" href="/assets/js/92.dc5800e2.js"><link rel="prefetch" href="/assets/js/93.4a77cabe.js"><link rel="prefetch" href="/assets/js/94.83769b9e.js"><link rel="prefetch" href="/assets/js/95.0388d28f.js"><link rel="prefetch" href="/assets/js/96.dacf6b86.js"><link rel="prefetch" href="/assets/js/97.dbba77db.js"><link rel="prefetch" href="/assets/js/98.5550b50a.js"><link rel="prefetch" href="/assets/js/99.b619ae82.js"><link rel="prefetch" href="/assets/js/vendors~docsearch.4b3b21bf.js"><link rel="prefetch" href="/assets/js/vendors~search-page.c3322d91.js">
    <link rel="stylesheet" href="/assets/css/0.styles.dbd5a077.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container vuemastery-promo"><div id="vm-banner" role="banner" class="vuemastery-banner hide"><div class="vuemastery-planet"></div> <a href="https://www.vuemastery.com/black-friday-2020" target="_blank" class="vuemastery-banner--link"><img src="/images/vuemastery/logo-vuemastery.svg" alt="vuemastery" class="vuemastery-banner--logo"> <div class="vuemastery-banner--wrapper"><p><span>Vue Mastery's Black Friday Sale:</span>Save 50% off a year of Vue courses</p> <button class="vuemastery-button">Get discount</button></div></a> <div class="vuemastery-banner--close"></div></div> <header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/logo.png" alt="Vue.js" class="logo"> <span class="site-name can-hide">Vue.js</span></a> <div class="links"><!----> <nav class="nav-links can-hide"><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="文档菜单" class="dropdown-title"><span class="title">文档</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/guide/introduction.html" class="nav-link">
  教程
</a></li><li class="dropdown-item"><!----> <a href="/style-guide/" class="nav-link">
  风格指南
</a></li><li class="dropdown-item"><!----> <a href="/cookbook/" class="nav-link">
  Cookbook
</a></li><li class="dropdown-item"><!----> <a href="/examples/markdown.html" class="nav-link">
  示例
</a></li><li class="dropdown-item"><!----> <a href="/guide/contributing/writing-guide.html" class="nav-link">
  贡献文档
</a></li><li class="dropdown-item"><!----> <a href="/guide/migration/introduction.html" class="nav-link">
  v3 迁移指南
</a></li></ul></div></div><div class="nav-item"><a href="/api/" class="nav-link router-link-active">
  API 参考
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="生态系统" class="dropdown-title"><span class="title">生态系统</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          社区
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/community/team/" class="nav-link">
  团队
</a></li><li class="dropdown-subitem"><a href="/community/partners.html" class="nav-link">
  合作伙伴
</a></li><li class="dropdown-subitem"><a href="/community/join/" class="nav-link">
  加入
</a></li><li class="dropdown-subitem"><a href="/community/themes/" class="nav-link">
  主题
</a></li></ul></li><li class="dropdown-item"><h4>
          官方项目
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="https://next.router.vuejs.org/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Vue Router
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-subitem"><a href="https://next.vuex.vuejs.org/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Vuex
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-subitem"><a href="https://cli.vuejs.org/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Vue CLI
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-subitem"><a href="https://vuejs.github.io/vue-test-utils-next-docs/guide/introduction.html" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Vue Test Utils
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-subitem"><a href="https://github.com/vuejs/vue-devtools" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Devtools
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-subitem"><a href="https://news.vuejs.org/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Weekly news
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="支持 Vue" class="dropdown-title"><span class="title">支持 Vue</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/support-vuejs/#one-time-donations" class="nav-link">
  一次性捐款
</a></li><li class="dropdown-item"><!----> <a href="/support-vuejs/#recurring-pledges" class="nav-link">
  周期性捐款
</a></li><li class="dropdown-item"><!----> <a href="https://www.smallsticker.com/%E8%B4%B4%E7%BA%B8/vue.html" target="_blank" rel="noopener noreferrer" class="nav-link external">
  贴纸
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://osholic.com/?utm_source=vue&amp;utm_medium=dropdown" target="_blank" rel="noopener noreferrer" class="nav-link external">
  周边
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://vue.threadless.com/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  T-Shirt 商店
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="多语言" class="dropdown-title"><span class="title">多语言</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://v3.vuejs.org" target="_blank" rel="noopener noreferrer" class="nav-link external">
  English
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <a href="https://github.com/vuejs/docs-next-zh-cn" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav> <div id="docsearch"></div></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="文档菜单" class="dropdown-title"><span class="title">文档</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/guide/introduction.html" class="nav-link">
  教程
</a></li><li class="dropdown-item"><!----> <a href="/style-guide/" class="nav-link">
  风格指南
</a></li><li class="dropdown-item"><!----> <a href="/cookbook/" class="nav-link">
  Cookbook
</a></li><li class="dropdown-item"><!----> <a href="/examples/markdown.html" class="nav-link">
  示例
</a></li><li class="dropdown-item"><!----> <a href="/guide/contributing/writing-guide.html" class="nav-link">
  贡献文档
</a></li><li class="dropdown-item"><!----> <a href="/guide/migration/introduction.html" class="nav-link">
  v3 迁移指南
</a></li></ul></div></div><div class="nav-item"><a href="/api/" class="nav-link router-link-active">
  API 参考
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="生态系统" class="dropdown-title"><span class="title">生态系统</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          社区
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/community/team/" class="nav-link">
  团队
</a></li><li class="dropdown-subitem"><a href="/community/partners.html" class="nav-link">
  合作伙伴
</a></li><li class="dropdown-subitem"><a href="/community/join/" class="nav-link">
  加入
</a></li><li class="dropdown-subitem"><a href="/community/themes/" class="nav-link">
  主题
</a></li></ul></li><li class="dropdown-item"><h4>
          官方项目
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="https://next.router.vuejs.org/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Vue Router
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-subitem"><a href="https://next.vuex.vuejs.org/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Vuex
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-subitem"><a href="https://cli.vuejs.org/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Vue CLI
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-subitem"><a href="https://vuejs.github.io/vue-test-utils-next-docs/guide/introduction.html" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Vue Test Utils
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-subitem"><a href="https://github.com/vuejs/vue-devtools" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Devtools
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-subitem"><a href="https://news.vuejs.org/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Weekly news
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="支持 Vue" class="dropdown-title"><span class="title">支持 Vue</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/support-vuejs/#one-time-donations" class="nav-link">
  一次性捐款
</a></li><li class="dropdown-item"><!----> <a href="/support-vuejs/#recurring-pledges" class="nav-link">
  周期性捐款
</a></li><li class="dropdown-item"><!----> <a href="https://www.smallsticker.com/%E8%B4%B4%E7%BA%B8/vue.html" target="_blank" rel="noopener noreferrer" class="nav-link external">
  贴纸
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://osholic.com/?utm_source=vue&amp;utm_medium=dropdown" target="_blank" rel="noopener noreferrer" class="nav-link external">
  周边
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://vue.threadless.com/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  T-Shirt 商店
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="多语言" class="dropdown-title"><span class="title">多语言</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://v3.vuejs.org" target="_blank" rel="noopener noreferrer" class="nav-link external">
  English
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <a href="https://github.com/vuejs/docs-next-zh-cn" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><a href="/api/application-config.html" class="sidebar-link">应用配置</a></li><li><a href="/api/application-api.html" aria-current="page" class="active sidebar-link">应用 API</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/api/application-api.html#component" class="sidebar-link">component</a></li><li class="sidebar-sub-header"><a href="/api/application-api.html#config" class="sidebar-link">config</a></li><li class="sidebar-sub-header"><a href="/api/application-api.html#directive" class="sidebar-link">directive</a></li><li class="sidebar-sub-header"><a href="/api/application-api.html#mixin" class="sidebar-link">mixin</a></li><li class="sidebar-sub-header"><a href="/api/application-api.html#mount" class="sidebar-link">mount</a></li><li class="sidebar-sub-header"><a href="/api/application-api.html#provide" class="sidebar-link">provide</a></li><li class="sidebar-sub-header"><a href="/api/application-api.html#unmount" class="sidebar-link">unmount</a></li><li class="sidebar-sub-header"><a href="/api/application-api.html#use" class="sidebar-link">use</a></li></ul></li><li><a href="/api/global-api.html" class="sidebar-link">全局 API</a></li><li><section class="sidebar-group depth-0"><a href="/api/options-api" class="sidebar-heading clickable"><span>选项</span> <!----></a> <ul class="sidebar-links sidebar-group-items"><li><a href="/api/options-data.html" class="sidebar-link">Data</a></li><li><a href="/api/options-dom.html" class="sidebar-link">DOM</a></li><li><a href="/api/options-lifecycle-hooks.html" class="sidebar-link">生命周期钩子</a></li><li><a href="/api/options-assets.html" class="sidebar-link">选项/资源</a></li><li><a href="/api/options-composition.html" class="sidebar-link">组合</a></li><li><a href="/api/options-misc.html" class="sidebar-link">杂项</a></li></ul></section></li><li><a href="/api/instance-properties.html" class="sidebar-link">实例 property</a></li><li><a href="/api/instance-methods.html" class="sidebar-link">实例方法</a></li><li><a href="/api/directives.html" class="sidebar-link">指令</a></li><li><a href="/api/special-attributes.html" class="sidebar-link">特殊指令</a></li><li><a href="/api/built-in-components.html" class="sidebar-link">内置组件</a></li><li><section class="sidebar-group depth-0"><a href="/api/reactivity-api" class="sidebar-heading clickable"><span>响应性 API</span> <!----></a> <ul class="sidebar-links sidebar-group-items"><li><a href="/api/basic-reactivity.html" class="sidebar-link">响应性基础 API</a></li><li><a href="/api/refs-api.html" class="sidebar-link">Refs</a></li><li><a href="/api/computed-watch-api.html" class="sidebar-link">Computed 与 watch</a></li></ul></section></li><li><a href="/api/composition-api.html" class="sidebar-link">组合式 API</a></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="应用-api"><a href="#应用-api" class="header-anchor">#</a> 应用 API</h1> <p>在 Vue 3 中，改变全局 Vue 行为的 API 现在被移动到了由新的 <code>createApp</code> 方法所创建的应用实例上。此外，现在它们的影响仅限于该特定应用实例：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> createApp <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'vue'</span>

<span class="token keyword">const</span> app <span class="token operator">=</span> <span class="token function">createApp</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>调用 <code>createApp</code> 返回一个应用实例。该实例提供了一个应用上下文。应用实例挂载的整个组件树共享相同的上下文，该上下文提供了之前在 Vue 2.x 中“全局”的配置。</p> <p>另外，由于 <code>createApp</code> 方法返回应用实例本身，因此可以在其后链式调用其它方法，这些方法可以在以下部分中找到。</p> <h2 id="component"><a href="#component" class="header-anchor">#</a> component</h2> <ul><li><p><strong>参数：</strong></p> <ul><li><code>{string} name</code></li> <li><code>{Function | Object} [definition]</code></li></ul></li> <li><p><strong>返回值：</strong></p> <ul><li>如果传入 <code>definition</code> 参数，返回应用实例。</li> <li>如果不传入 <code>definition</code> 参数，返回组件定义。</li></ul></li> <li><p><strong>用法：</strong></p> <p>注册或检索全局组件。注册还会使用给定的 <code>name</code> 参数自动设置组件的 <code>name</code>。</p></li> <li><p><strong>示例：</strong></p></li></ul> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> createApp <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'vue'</span>

<span class="token keyword">const</span> app <span class="token operator">=</span> <span class="token function">createApp</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token comment">// 注册一个名为my-component的组件</span>
app<span class="token punctuation">.</span><span class="token function">component</span><span class="token punctuation">(</span><span class="token string">'my-component'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
  <span class="token comment">/* ... */</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token comment">// 检索注册的组件(始终返回构造函数)</span>
<span class="token keyword">const</span> MyComponent <span class="token operator">=</span> app<span class="token punctuation">.</span><span class="token function">component</span><span class="token punctuation">(</span><span class="token string">'my-component'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><ul><li><strong>参考：</strong><a href="/guide/component-basics.html">组件基础</a></li></ul> <h2 id="config"><a href="#config" class="header-anchor">#</a> config</h2> <ul><li><strong>用法：</strong></li></ul> <p>包含应用配置的对象。</p> <ul><li><strong>示例：</strong></li></ul> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> createApp <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'vue'</span>
<span class="token keyword">const</span> app <span class="token operator">=</span> <span class="token function">createApp</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>

app<span class="token punctuation">.</span>config <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token operator">...</span><span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><ul><li><strong>参考：</strong><a href="/api/application-config.html">应用配置</a></li></ul> <h2 id="directive"><a href="#directive" class="header-anchor">#</a> directive</h2> <ul><li><p><strong>参数：</strong></p> <ul><li><code>{string} name</code></li> <li><code>{Function | Object} [definition]</code></li></ul></li> <li><p><strong>返回值：</strong></p> <ul><li>如果传入 <code>definition</code> 参数，返回应用实例。</li> <li>如果不传入 <code>definition</code> 参数，返回指令定义。</li></ul></li> <li><p><strong>用法：</strong></p> <p>注册或检索全局指令。</p></li> <li><p><strong>示例：</strong></p></li></ul> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> createApp <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'vue'</span>
<span class="token keyword">const</span> app <span class="token operator">=</span> <span class="token function">createApp</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token comment">// 注册</span>
app<span class="token punctuation">.</span><span class="token function">directive</span><span class="token punctuation">(</span><span class="token string">'my-directive'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
  <span class="token comment">// 指令是具有一组生命周期的钩子：</span>
  <span class="token comment">// 在绑定元素的父组件挂载之前调用</span>
  <span class="token function">beforeMount</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token comment">// 绑定元素的父组件挂载时调用</span>
  <span class="token function">mounted</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token comment">// 在包含组件的 VNode 更新之前调用</span>
  <span class="token function">beforeUpdate</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token comment">// 在包含组件的 VNode 及其子组件的 VNode 更新之后调用</span>
  <span class="token function">updated</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token comment">// 在绑定元素的父组件卸载之前调用</span>
  <span class="token function">beforeUnmount</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token comment">// 卸载绑定元素的父组件时调用</span>
  <span class="token function">unmounted</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token comment">// 注册 (功能指令)</span>
app<span class="token punctuation">.</span><span class="token function">directive</span><span class="token punctuation">(</span><span class="token string">'my-directive'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token comment">// 这将被作为 `mounted` 和 `updated` 调用</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token comment">// getter, 如果已注册，则返回指令定义</span>
<span class="token keyword">const</span> myDirective <span class="token operator">=</span> app<span class="token punctuation">.</span><span class="token function">directive</span><span class="token punctuation">(</span><span class="token string">'my-directive'</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br></div></div><p>指令钩子传递了这些参数：</p> <h4 id="el"><a href="#el" class="header-anchor">#</a> el</h4> <p>指令绑定到的元素。这可用于直接操作 DOM。</p> <h4 id="binding"><a href="#binding" class="header-anchor">#</a> binding</h4> <p>包含以下 property 的对象。</p> <ul><li><code>instance</code>：使用指令的组件实例。</li> <li><code>value</code>：传递给指令的值。例如，在 <code>v-my-directive=&quot;1 + 1&quot;</code> 中，该值为 <code>2</code>。</li> <li><code>oldValue</code>：先前的值，仅在 <code>beforeUpdate</code> 和 <code>updated</code> 中可用。值是否已更改都可用。</li> <li><code>arg</code>：参数传递给指令 (如果有)。例如在 <code>v-my-directive:foo</code> 中，arg 为 <code>&quot;foo&quot;</code>。</li> <li><code>modifiers</code>：包含修饰符 (如果有) 的对象。例如在 <code>v-my-directive.foo.bar</code> 中，修饰符对象为 <code>{foo: true，bar: true}</code>。</li> <li><code>dir</code>：一个对象，在注册指令时作为参数传递。例如，在以下指令中</li></ul> <div class="language-js line-numbers-mode"><pre class="language-js"><code>app<span class="token punctuation">.</span><span class="token function">directive</span><span class="token punctuation">(</span><span class="token string">'focus'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
  <span class="token function">mounted</span><span class="token punctuation">(</span><span class="token parameter">el</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    el<span class="token punctuation">.</span><span class="token function">focus</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><code>dir</code> 将会是以下对象：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">{</span>
  <span class="token function">mounted</span><span class="token punctuation">(</span><span class="token parameter">el</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    el<span class="token punctuation">.</span><span class="token function">focus</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h4 id="vnode"><a href="#vnode" class="header-anchor">#</a> vnode</h4> <p>上面作为 el 参数收到的真实 DOM 元素的蓝图。</p> <h4 id="prevnode"><a href="#prevnode" class="header-anchor">#</a> prevNode</h4> <p>上一个虚拟节点，仅在 <code>beforeUpdate</code> 和 <code>updated</code> 钩子中可用。</p> <div class="custom-block tip"><p class="custom-block-title">Note</p> <p>除了 <code>el</code> 之外，你应该将这些参数视为只读，并且永远不要修改它们。如果你需要跨钩子共享信息，建议通过元素的<a href="https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLElement/dataset" target="_blank" rel="noopener noreferrer">自定义数据属性集<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>进行共享。</p></div> <ul><li><strong>参考：</strong><a href="/guide/custom-directive.html">自定义指令</a></li></ul> <h2 id="mixin"><a href="#mixin" class="header-anchor">#</a> mixin</h2> <ul><li><p><strong>参数：</strong></p> <ul><li><code>{Object} mixin</code></li></ul></li> <li><p><strong>返回值：</strong></p> <ul><li>应用实例</li></ul></li> <li><p><strong>用法：</strong></p> <p>在整个应用范围内应用混入。一旦注册，它们就可以在当前的应用中任何组件模板内使用它。插件作者可以使用此方法将自定义行为注入组件。<strong>不建议在应用代码中使用</strong>。</p></li> <li><p><strong>参考：</strong><a href="/guide/mixins.html#全局混入">全局混入</a></p></li></ul> <h2 id="mount"><a href="#mount" class="header-anchor">#</a> mount</h2> <ul><li><p><strong>参数：</strong></p> <ul><li><code>{Element | string} rootContainer</code></li> <li><code>{boolean} isHydrate</code></li></ul></li> <li><p><strong>返回值：</strong></p> <ul><li>根组件实例</li></ul></li> <li><p><strong>用法：</strong></p> <p>将应用实例的根组件挂载在提供的 DOM 元素上。</p></li> <li><p><strong>示例：</strong></p></li></ul> <div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>body</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>my-app<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>body</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> createApp <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'vue'</span>

<span class="token keyword">const</span> app <span class="token operator">=</span> <span class="token function">createApp</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token comment">// 做一些必要的准备</span>
app<span class="token punctuation">.</span><span class="token function">mount</span><span class="token punctuation">(</span><span class="token string">'#my-app'</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><ul><li><strong>参考：</strong> <ul><li><a href="/guide/instance.html#生命周期图示">生命周期图示</a></li></ul></li></ul> <h2 id="provide"><a href="#provide" class="header-anchor">#</a> provide</h2> <ul><li><p><strong>参数：</strong></p> <ul><li><code>{string | Symbol} key</code></li> <li><code>value</code></li></ul></li> <li><p><strong>返回值：</strong></p> <ul><li>应用实例</li></ul></li> <li><p><strong>用法：</strong></p> <p>设置一个可以被注入到应用范围内所有组件中的值。组件应该使用 <code>inject</code> 来接收 provide 的值。</p> <p>从 <code>provide</code>/<code>inject</code> 的角度来看，可以将应用程序视为根级别的祖先，而根组件是其唯一的子级。</p> <p>该方法不应该与 <a href="/api/options-composition.html#provide-inject">provide 组件选项</a>或组合式 API 中的 <a href="/api/composition-api.html#provide-inject">provide 方法</a>混淆。虽然它们也是相同的 <code>provide</code>/<code>inject</code> 机制的一部分，但是是用来配置组件 provide 的值而不是应用 provide 的值。</p> <p>通过应用提供值在写插件时尤其有用，因为插件一般不能使用组件提供值。这是使用 <a href="/api/application-config.html#globalProperties">globalProperties</a> 的替代选择。</p> <div class="custom-block tip"><p class="custom-block-title">Note</p> <p><code>provide</code> 和 <code>inject</code> 绑定不是响应式的。这是有意为之。不过，如果你向下传递一个响应式对象，这个对象上的 property 会保持响应式。</p></div></li> <li><p><strong>示例：</strong></p> <p>向根组件中注入一个 property，值由应用提供。</p></li></ul> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> createApp <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'vue'</span>

<span class="token keyword">const</span> app <span class="token operator">=</span> <span class="token function">createApp</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  inject<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">'user'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
  template<span class="token operator">:</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string"><span class="language-html">
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">&gt;</span></span>
      <span class="token interpolation"><span class="token punctuation">{{</span><span class="token inline-js language-js"> user </span><span class="token punctuation">}}</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
  </span></span><span class="token template-punctuation string">`</span></span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

app<span class="token punctuation">.</span><span class="token function">provide</span><span class="token punctuation">(</span><span class="token string">'user'</span><span class="token punctuation">,</span> <span class="token string">'administrator'</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><ul><li><strong>参考：</strong> <ul><li><a href="/guide/component-provide-inject.html">Provide / Inject</a></li></ul></li></ul> <h2 id="unmount"><a href="#unmount" class="header-anchor">#</a> unmount</h2> <ul><li><p><strong>参数：</strong></p> <ul><li><code>{Element | string} rootContainer</code></li></ul></li> <li><p><strong>用法：</strong></p> <p>在提供的 DOM 元素上卸载应用实例的根组件。</p></li> <li><p><strong>示例：</strong></p></li></ul> <div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>body</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>my-app<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>body</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> createApp <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'vue'</span>

<span class="token keyword">const</span> app <span class="token operator">=</span> <span class="token function">createApp</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token comment">// 做一些必要的准备</span>
app<span class="token punctuation">.</span><span class="token function">mount</span><span class="token punctuation">(</span><span class="token string">'#my-app'</span><span class="token punctuation">)</span>

<span class="token comment">// 挂载5秒后，应用将被卸载</span>
<span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> app<span class="token punctuation">.</span><span class="token function">unmount</span><span class="token punctuation">(</span><span class="token string">'#my-app'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">5000</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h2 id="use"><a href="#use" class="header-anchor">#</a> use</h2> <ul><li><p><strong>参数：</strong></p> <ul><li><code>{Object | Function} plugin</code></li> <li><code>...options (可选)</code></li></ul></li> <li><p><strong>返回值：</strong></p> <ul><li>应用实例</li></ul></li> <li><p><strong>用法：</strong></p> <p>安装 Vue.js 插件。如果插件是一个对象，它必须暴露一个 <code>install</code> 方法。如果它本身是一个函数，它将被视为安装方法。</p> <p>该安装方法将以应用实例作为第一个参数被调用。传给 <code>use</code> 的其他 <code>options</code> 参数将作为后续参数传入该安装方法。</p> <p>当在同一个插件上多次调用此方法时，该插件将仅安装一次。</p></li> <li><p><strong>参考：</strong> <a href="/guide/plugins.html">插件</a></p></li></ul></div> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/api/application-config.html" class="prev">应用配置</a></span> <span class="next"><a href="/api/global-api.html">全局 API</a>
      →
    </span></p></div> </main></div><div class="global-ui"><!----></div></div>
    <script src="/assets/js/app.8e9a370d.js" defer></script><script src="/assets/js/11.282d7961.js" defer></script><script src="/assets/js/40.5c7a2259.js" defer></script><script src="/assets/js/22.0b365510.js" defer></script>
  </body>
</html>
